package 力扣._202_快乐数;

import java.util.HashSet;
import java.util.Set;

public class Solution {

    int tool(int n) {
        int sum = 0;
        while (n > 0) {
            int t = n % 10;
            sum += t * t;
            n /= 10;
        }
        return sum;
    }

    public boolean isHappy(int n) {
        Set<Integer> set = new HashSet<>();
        set.add(n);
        while (true){
            n = tool(n);
            if(n==1)
                return true;
            if(set.contains(n))
                return false;
            else
                set.add(n);
        }
    }

    public static void main(String[] args) {
        boolean happy = new Solution().isHappy(2);
        System.out.println(happy);
    }
}
